Skip to content

stream: emit end when paused on the last chunk - #67769

Closed
christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:stream-pipe-end-backpressure
Closed

christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:stream-pipe-end-backpressure

Conversation

@christianaurichzm

Copy link
Copy Markdown
Member

Since v26.11.0, a flowing Readable doesn't emit 'end' when _read() pushes EOF in the call after the last chunk and the consumer pauses on that chunk:

const { Readable } = require('node:stream');
const chunks = [Buffer.from('a'), Buffer.from('b')];
const r = new Readable({ read() { this.push(chunks.shift() ?? null); } });
r.on('data', (chunk) => { if (chunk.toString() === 'b') r.pause(); });
r.on('end', () => console.log('end'));

This prints end on v22 and nothing on main. pipe() pauses there when write() returns false, so the destination isn't ended unless it drains again.

flowSync() from #66182 runs _read() before delivering each chunk, like read(), but skips the endReadable() that read() schedules when that _read() hits EOF. This schedules it at the same point.

Thanks to @AZMCode for tracing this to flowSync().

Fixes: #67762

flowSync() calls _read() before delivering each chunk, like read(),
but did not schedule 'end' when that call pushed EOF and nothing was
left to deliver. A consumer that paused on the last chunk, such as
pipe() after write() returned false, then got no 'end' until it
resumed. Schedule endReadable() there, as read() does.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams. labels Nov 25, 2026
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.55%. Comparing base (36ca6a7) to head (7ca762d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67769   +/-   ##
=======================================
  Coverage   90.54%   90.55%           
=======================================
  Files         803      803           
  Lines      285302   285307    +5     
  Branches    55074    55071    -3     
=======================================
+ Hits       258324   258354   +30     
+ Misses      17104    17099    -5     
+ Partials     9874     9854   -20     
Files with missing lines Coverage Δ
lib/internal/streams/readable.js 98.98% <100.00%> (+<0.01%) ⬆️

... and 26 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina

Copy link
Copy Markdown
Member

While I'm sorry for the breakage, but I think the current behavior is correct.

@AZMCode

AZMCode commented Nov 25, 2026 •

Copy link
Copy Markdown

Hi there!

After chatting a bit in my agent session, it appears that indeed this bug isn't the right one, the other related issue is.

This change did change observable behavior, which is why I ended up flagging it in the first place.

Apologies for the mistake.

Here's a better AI overview than mine:

Thanks for looking at this, that makes sense. I checked: before v26.11 a paused stream only emitted 'end' when _read() pushed synchronously; with an async source it never did, on any version. So v26.11 makes the behavior consistent, and I agree it isn't the bug.

The underlying problem is in the HTTP client: once the response is complete, socketOnData() removes the socket's 'drain' listener even if the request is still waiting for it, so a piped body never calls req.end(). With an async source piped into an HTTPS request, this strands requests on v20, v24 and v26.10 as well, with no flowSync() involved. Tracked in #67763.

It may still be worth a changelog note for v26.11, since it changed observable behavior for sync sources (this broke @azure/storage-blob uploads via node-fetch and @actions/artifact).

@christianaurichzm

Copy link
Copy Markdown
Member Author

Makes sense, thanks. Async sources never got 'end' there either, so it wasn't really something to rely on. The hang is the HTTP client dropping 'drain' once the response is done (#67763).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stream: pipe() no longer ends the destination when EOF follows a backpressured last chunk (regression in v26.11.0)

4 participants